import { loginAPI } from '@/apis/user'
import router from '@/router'
import { getToken, setToken } from '@/utils/auth'
import { Message } from 'element-ui'

export default {
  namespaced: true,
  state: {
    token: getToken() || ''
  },
  mutations: {
    setTokenFn(state, token) {
      state.token = token
      setToken(token)

      Message.success('登录成功')
      if (router.app.$route.query.redirect) {
        router.push(router.app.$route.query.redirect)
      } else {
        router.push('/')
      }
    }
  },
  actions: {
    async requestTokenFn(conent, data) {
      const res = await loginAPI(data)
      conent.commit('setTokenFn', res.data.token)
    }
  }
}
